package com.atguigu.controller;

import com.atguigu.client.*;
import com.atguigu.entity.SkuInfo;
import com.atguigu.result.RetVal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class WebSeckillController {

    @Autowired
    SeckillFeignClient seckillFeignClient;
    //实现秒杀页面的跳转
    @GetMapping("/seckill-index.html")
    public String seckillIndex(Model model){
        RetVal result = seckillFeignClient.queryAllSeckillProduct();
        model.addAttribute("list",result.getData());
        return "seckill/index";
    }

    //2.根据skuId 获取秒杀对象数据
    @GetMapping("/seckill-detail/{skuId}.html")
    public String seckillDetail(@PathVariable Long skuId, Model model){
        //获取商品详情数据
        RetVal result = seckillFeignClient.getSeckillProductBySkuId(skuId);
        model.addAttribute("item",result.getData());
        return "seckill/detail";
    }

    //3.获取下单码成功之后要访问的页面
    @GetMapping("/seckill-queue.html")
    public String seckillQueue(Long skuId,String seckillCode,Model model){
        model.addAttribute("skuId",skuId);
        model.addAttribute("seckillCode",seckillCode);
        return "seckill/queue";
    }

    //4.获取下单码成功之后要访问的页面
    @GetMapping("/seckill-confirm.html")
    public String seckillConfirm(Model model) {
        RetVal retVal = seckillFeignClient.seckillConfirm();
        if (retVal.isOk()) {
            Map<String, Object> result = (Map<String, Object>) retVal.getData();
            model.addAllAttributes(result);
            return "seckill/confirm";
        } else {
            model.addAttribute("message", retVal.getMessage());
            return "seckill/confirm";
        }

    }

}


